﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using InstallCheckFramework.Properties;

namespace InstallCheckFramework
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] frameworkList = GetDotNetVersions();
            bool hasFramework = false;
            foreach (var s in frameworkList)
            {
                //MessageBox.Show(s);
                if(s.StartsWith("3.5"))
                {
                    hasFramework = true;
                    break;
                }
            }
            if(hasFramework)
            {
                try
                {
                    var fileByteCollection =
                Resources.ResourceManager.GetObject("WizardTourDemo");
                    if (null == fileByteCollection) return;
                    var byteCollection = fileByteCollection as byte[];
                    if (null == byteCollection) return;
                    var installPath = Path.Combine(Path.GetTempPath(), "FilemanagerInstallFolder");

                    if (Directory.Exists(installPath))
                        Directory.Delete(installPath,true);
                        Directory.CreateDirectory(installPath);

                    var bonusSkinsPath = Path.Combine(installPath, "DevExpress.BonusSkins.v12.1.dll");
                    File.WriteAllBytes(bonusSkinsPath, Resources.DevExpress_BonusSkins_v12_1);

                    var PrintingPath = Path.Combine(installPath, "DevExpress.Printing.v12.1.Core.dll");
                    File.WriteAllBytes(PrintingPath, Resources.DevExpress_Printing_v12_1_Core);

                    var DataPath = Path.Combine(installPath, "DevExpress.Data.v12.1.dll");
                    File.WriteAllBytes(DataPath, Resources.DevExpress_Data_v12_1);

                    var UtilsPath = Path.Combine(installPath, "DevExpress.Utils.v12.1.dll");
                    File.WriteAllBytes(UtilsPath, Resources.DevExpress_Utils_v12_1);

                    var XtraEditorsPath = Path.Combine(installPath, "DevExpress.XtraEditors.v12.1.dll");
                    File.WriteAllBytes(XtraEditorsPath, Resources.DevExpress_XtraEditors_v12_1);

                    var XtraWizardPath = Path.Combine(installPath, "DevExpress.XtraWizard.v12.1.dll");
                    File.WriteAllBytes(XtraWizardPath, Resources.DevExpress_XtraWizard_v12_1);

                    var paFormsPath = Path.Combine(installPath, "Paway.Windows.Forms.dll");
                    File.WriteAllBytes(paFormsPath, Resources.Paway_Windows_Forms);

                    var paHelperPath = Path.Combine(installPath, "Paway.Windows.Helper.dll");
                    File.WriteAllBytes(paHelperPath, Resources.Paway_Windows_Helper);

                    var paResourcePath = Path.Combine(installPath, "Paway.Windows.Resource.dll");
                    File.WriteAllBytes(paResourcePath, Resources.Paway_Windows_Resource);

                    var paWin32Path = Path.Combine(installPath, "Paway.Windows.Win32.dll");
                    File.WriteAllBytes(paWin32Path, Resources.Paway_Windows_Win32);

                    var icSharpCodeSharpZipLibPath = Path.Combine(installPath, "ICSharpCode.SharpZipLib.dll");
                    File.WriteAllBytes(icSharpCodeSharpZipLibPath, Resources.ICSharpCode_SharpZipLib);

                    var fileManagerGlobalLibrary = Path.Combine(installPath, "FileManagerGlobal_Library.dll");
                    File.WriteAllBytes(fileManagerGlobalLibrary, Resources.FileManagerGlobal_Library);

                    var interopIWshRuntimeLibrary = Path.Combine(installPath, "Interop.IWshRuntimeLibrary.dll");
                    File.WriteAllBytes(interopIWshRuntimeLibrary, Resources.Interop_IWshRuntimeLibrary);

                    var installFile = Path.Combine(installPath, "FilemanagerClientInstall.exe");
                    if (File.Exists(installFile))
                        File.Delete(installFile);
                    File.WriteAllBytes(installFile, Resources.WizardTourDemo);
                    var processStartInfo = new ProcessStartInfo {FileName = installFile, WorkingDirectory = installPath};
                    Process.Start(processStartInfo);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "安装错误", MessageBoxButtons.OK,
                                 MessageBoxIcon.Error);
                }
                
            }
            else
            {
                MessageBox.Show("请安装 .net framework 3.5版本后再安装本程序!", "安装错误", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                Process.Start("Explorer.exe", "ftp://10.194.5.190");
            }
            Environment.Exit(Environment.ExitCode);
        }

        //C#获取已安装 .NET Framework 版本
        public string[] GetDotNetVersions()
        {
            var directories = new DirectoryInfo(Environment.SystemDirectory + @"\..\Microsoft.NET\Framework").GetDirectories("v?.?.*");
            var list = new ArrayList();
            foreach (var info2 in directories)
            {
                list.Add(info2.Name.Substring(1));
            }
            return (list.ToArray(typeof(string)) as string[]);
        }
        #region hide application
        private bool _windowCreate = true;
        protected override void OnActivated(EventArgs e)
        {

            if (_windowCreate)
            {
                base.Visible = false;
                _windowCreate = false;
            }
            base.OnActivated(e);
        }
        protected override CreateParams CreateParams
        {
            get
            {
                var WS_EX_TOOLWINDOW = 0x80;
                var CP = base.CreateParams;
                CP.ExStyle = CP.ExStyle | WS_EX_TOOLWINDOW;
                return CP;
            }
        }
        #endregion
    }
}
